package com.ryskoo.customer.customeraddress.service;

import com.ryskoo.customer.customeraddress.domain.CustomerAddressPO;
import com.ryskoo.customer.customeraddress.repository.ICustomerAddressRepository;
import com.ryskoo.framework.core.bean.QueryConditionEnum;
import com.ryskoo.framework.core.bean.QueryMessageDTO;
import com.ryskoo.framework.core.exception.InvalidParamException;
import com.ryskoo.framework.core.service.BasicServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 会员收货地址业务接口实现
 *
 * @author thq
 */
@Service
@Transactional
public class CustomerAddressServiceImpl extends BasicServiceImpl<CustomerAddressPO> implements ICustomerAddressService {

    @Autowired
    private ICustomerAddressRepository customerAddressRepository;

    @PostConstruct
    public void init() {
        this.setDao(customerAddressRepository);
    }

    @Override
    public void create(CustomerAddressPO customerAddress) {
        checkBeforeCreate(customerAddress);
        // 保证用户只有一个默认地址
        setDefaultAddress(customerAddress, false);

        super.create(customerAddress);
    }

    private void checkBeforeCreate(CustomerAddressPO customerAddress) throws InvalidParamException {
        checkCustomerId(customerAddress.getCustomerId());
    }

    @Override
    public void update(CustomerAddressPO customerAddress) {
        checkBeforeUpdate(customerAddress);
        // 保证用户只有一个默认地址
        setDefaultAddress(customerAddress, true);

        super.update(customerAddress);
    }

    private void checkBeforeUpdate(CustomerAddressPO customerAddress) throws InvalidParamException {
        checkCustomerId(customerAddress.getCustomerId());
    }

    private void checkCustomerId(String customerId) throws InvalidParamException {
        if (StringUtils.isBlank(customerId)) {
            throw new InvalidParamException("会员id为空");
        }
    }

    private void setDefaultAddress(CustomerAddressPO customerAddress, boolean isUpdate) {
        // 查询会员已有的收货地址
        QueryMessageDTO queryMessage = new QueryMessageDTO();
        queryMessage.addCondition("customerId", QueryConditionEnum.EQ.name(), customerAddress.getCustomerId());
        queryMessage.addCondition("defaultAddress", QueryConditionEnum.EQ.name(), true);
        if (isUpdate) {
            queryMessage.addCondition("uuid", QueryConditionEnum.NE.name(), customerAddress.getUuid());
        }
        List<CustomerAddressPO> addressList = customerAddressRepository.queryByCondition(queryMessage, false);
        if (customerAddress.isDefaultAddress()) {
            if (null != addressList && addressList.size() > 0) {
                for (CustomerAddressPO address : addressList) {
                    address.setDefaultAddress(false);
                }
                customerAddressRepository.batchUpdate(addressList);
            }
        } else {
            if (null == addressList || addressList.isEmpty()) {
                customerAddress.setDefaultAddress(true);
            }
        }
    }
}
